﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Repository;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using System.Linq.Expressions;

namespace GoodMan.Portal.Biz
{
    public class DormitoryBiz
    {
        public static ItemListBox<Dormitory> GetAllDormitory(Expression<Func<T_Dormitory, bool>> where, int page, int takeSize)
        {
            var count = new Dormitory().Count(where);
            using (Context ctx = new Context())
            {
                var data = ctx.Dormitory
                           .Where(where)
                           .OrderByDescending(c => c.Id)
                           .Skip(page * takeSize)
                           .Take(takeSize)
                           .ToList()
                           .Select(c => new Dormitory().Set(c))
                           .ToList();
                var list = new ItemListBox<Dormitory>(data);
                return list;
            }
        }

        public static ItemListBox<Dormitory> GetAllDormitory(int page, int takeSize, string search = "")
        {
            using (Context ctx = new Context())
            {
                var query = from c in ctx.Dormitory
                            select c;

                if (!string.IsNullOrEmpty(search))//楼栋名称
                {
                    query = query.Where(c => c.Name.Contains(search));
                }
                query = query.Where(c => c.CollegeId == 1);
                var data = query.OrderByDescending(c => c.CreateDate).Skip(page * takeSize).Take(takeSize).ToList().Select(c => new Dormitory().Set(c)).ToList();
                var itemCount = query.Count();
                return new ItemListBox<Dormitory>(data).BuildPage(itemCount, page, takeSize, new PageParameter()
                {
                    Style = "workspace"
                });
            }
        }

        /// <summary>
        /// 验证楼栋是否存在
        /// </summary>
        /// <param name="name">楼栋名</param>
        /// <returns>true:不存在 false:存在</returns>
        public static bool IsExits(string name) { 
            if(new Dormitory().Any(c=>c.Name.Equals(name)))
            {
                return false;
            }
            return true;
        }


        public static Dormitory SaveNewDormitory(int collegeId, string name, string creator)
        {                 
            Dormitory dormitory=
                new Dormitory()
                {
                    Name=name,
                    CollegeId=collegeId,
                    Creator=creator,
                    CreateDate=DateTime.Now
                        
                };
            return dormitory.PostAdd();
        }

        public static bool Delete(int id)
        {
            try
            {
                var dormitory = new Dormitory().Find(id.ToString());
                if (dormitory != null)
                {
                    return dormitory.PostDelete() != null;
                }
            }
            catch (Exception ex)
            {
                //日志记录
                return false;
            }
            return false;
        }
    }
}
